package com.quwan.manager.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.quwan.common.query.LambdaQueryWrapperX;
import com.quwan.common.result.PageResult;
import com.quwan.entity.TAdmin;
import com.quwan.entity.TRole;
import com.quwan.entity.TRoleMenu;
import com.quwan.manager.service.RoleService;
import com.quwan.mapper.TRoleMapper;
import com.quwan.mapper.TRoleMenuMapper;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * @author laozhang
 * @date 2021/9/25 11:54
 */
@Service
@RequiredArgsConstructor
public class RoleServiceImpl implements RoleService {

    private final TRoleMapper roleMapper;
    private final TRoleMenuMapper roleMenuMapper;

    @Override
    public PageResult<TRole> getRoleListPage(int page, int size,Long id, String roleName) {
        LambdaQueryWrapperX<TRole> wrapper = new LambdaQueryWrapperX<TRole>()
                .orderByDesc(TRole::getAddTime);
        if (id !=null) {
            wrapper.eq(TRole::getId,id);
        }
        if (StringUtils.isNotBlank(roleName)) {
            wrapper.like(TRole::getRoleName,roleName);
        }


        Page<TRole> rolePage = roleMapper.selectPage(new Page<>(page, size), wrapper);
        PageResult<TRole> result = new PageResult<TRole>()
                .setCurrent(rolePage.getCurrent())
                .setPages(rolePage.getPages())
                .setSize(rolePage.getSize())
                .setTotal(rolePage.getTotal())
                .setRecords(rolePage.getRecords());
        return result;
    }

    @Override
    public List<TRole> getRoleList(Long id,String roleName) {
        LambdaQueryWrapperX<TRole> wrapper = new LambdaQueryWrapperX<TRole>()
                .orderByDesc(TRole::getAddTime);
        if (StringUtils.isNotBlank(id+"")) {
            wrapper.eq(TRole::getId,id);
        }
        if (StringUtils.isNotBlank(roleName)) {
            wrapper.like(TRole::getRoleName,roleName);
        }

        List<TRole> res = roleMapper.selectList(wrapper);
        return res;
    }

    @Override
    public boolean addRole(TRole role) {
        return roleMapper.insert(role) == 1;
    }

    @Override
    public boolean updateRole(TRole role) {
        return roleMapper.updateById(role) == 1;
    }

    @Override
    public TRole detailRole(Integer id) {
        return roleMapper.selectById(id);
    }

    @Override
    public boolean delRole(Integer id) {
        return roleMapper.deleteById(id) == 1;
    }

    @Override
    public Map<String, Object> getRoleMenuList(Integer roleId) {
        return null;
    }

    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)
    public void configPower(Integer roleId, String menuIds) {
        String[] menuArray = menuIds.split(",");
        List<TRoleMenu> rmList = new ArrayList<>();
        for (String menuId: menuArray) {
            TRoleMenu rm = new TRoleMenu();
            rm.setRoleId(roleId);
            rm.setMenuId(Integer.valueOf(menuId));
            rmList.add(rm);
        }
        //删除原关联数据
        roleMenuMapper.deleteRoleMenu(roleId);
        //增加关联数据
        roleMenuMapper.insertRoleMenuList(rmList);
    }
}
